package com.atguigu.stack;

public class Calculator {
    public static void main(String[] args) {
        System.out.println(42=='*');
        //数字栈
        StackArray2 stackArray2 = new StackArray2(10);
        //符号栈
        StackArray2 stackArray3 =  new StackArray2(10);
        String expression = "232+3*6-1";
        //用来遍历表达式
        int index = 0;
        int i;
        char chuan = ' ';
        String shuzi = "";
        while (true){
            char c = expression.substring(index, index + 1).charAt(0);
            i = stackArray2.operPriority(c);
            //判断为数字还是符号
            if (i==-1){
                //char转int要减48
//                stackArray2.push(c-48);
                shuzi += c;
                int temp = index;
                temp++;
                if (temp == expression.length()){
                    int i1 = Integer.parseInt(shuzi);
                    stackArray2.push(i1);
                    shuzi="";
                }else {
                    char su = expression.substring(temp, temp + 1).charAt(0);
                    if (stackArray2.operPriority(su) != -1) {
                        int i1 = Integer.parseInt(shuzi);
                        stackArray2.push(i1);
                        shuzi = "";
                    }
                }
                //处理多位数情况
            }else {
                //符号栈为空，可以直接插入
                if (stackArray3.isEmpty()){
                    stackArray3.push(c);
                }else {
                    //char可以直接应用到int变量里，
                    if (stackArray3.operPriority(c)<= stackArray3.operPriority(stackArray3.peek())){
                        int num1 = stackArray2.pop();
                        int num2 = stackArray2.pop();
                        int cc = stackArray3.pop();
                        int cal = stackArray2.cal(num1, num2, cc);
                        stackArray2.push(cal);
                        stackArray3.push(c);
                    }else {
                        stackArray3.push(c);
                    }
                }
            }
            index++;
            if (index == expression.length()){
                break;
            }
        }

        while (true){
            if (stackArray3.isEmpty()){
                System.out.printf("计算结果为%d",stackArray2.peek());
                break;
            }
            int num1 = stackArray2.pop();
            int num2 = stackArray2.pop();
            int c = stackArray3.pop();
            int cal = stackArray2.cal(num1, num2, c);
            stackArray2.push(cal);

        }
    }
}

class StackArray2{
    //定义stack最大值
    int max;
    int arry[];
    //定义一个栈顶指针
    int top = -1;


    public StackArray2(int max){
        this.max = max;
        arry = new int[max];
    }

    //获取栈顶元素
    public int peek(){
        return arry[top];
    }

    //判断stack是否为空
    public boolean isEmpty(){
        return top == -1;
    }

    //判断stack是否已满
    public boolean isFull(){
        return top == max-1;
    }

    public void push(int val){
        if (isFull()){
            System.out.println("栈已满，无法插入数据");
            return;
        }
        top++;
        arry[top]=val;
    }

    //从栈中取出数据
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈已空无法取出数据");

        }
        //调整栈顶的序号，后续入栈的时候会覆盖掉没有真实删除的数据
        int var = arry[top];
        top--;
        return var;
    }

    //展示栈中所有的数据
    public void list(){
        if (isEmpty()){
            System.out.printf("栈已空，无数据展示");
            return;
        }
        int temp = top;
        for (int i=temp;temp>=0;temp--){
            System.out.println(arry[temp]);
        }
    }

    //判断符合优先级
    public static int operPriority(int str){
        if (str == '+' || str == '-' ){
            return 1;
        }else if (str == '*' || str == '/' ){
            return 2;
        }else {
            return -1;
        }
    }

    //计算逻辑
    public int cal(int num1,int num2,int c){
        int res = 0;
        switch (c){
            case '+':
                res = num1+num2;
                break;
            case '-':
                res = num2-num1;
                break;
            case '*':
                res = num1*num2;
                break;
            case '/':
                res = num2/num1;
                break;
        }
        return res;

    }

}